
using Org.Loon.Framework.Xna.Game.Core;
using System;
using Org.Loon.Framework.Xna.Java;
using Org.Loon.Framework.Xna.Game.Core.Graphics.OpenGL;

namespace Org.Loon.Framework.Xna.Game.Action.IMovie
{
    public abstract class MovieSequence : LRelease
    {

        private int frame = 0;

        private int frames = 0;

        private string name;

        private bool playing = false;

        private Movie movie;

        public MovieSequence(int frames)
            : this(Convert.ToString(JavaRuntime.CurrentTimeMillis()), frames)
        {

        }

        public MovieSequence(string name, int frames)
        {
            this.name = name;
            this.frames = frames;
        }

        public abstract void Handle(int idx);

        public void Start()
        {
            this.playing = true;
            if (IsFinished())
            {
                this.frame = 0;
            }
        }

        public void Stop()
        {
            this.playing = false;
        }

        public bool IsFinished()
        {
            return this.frame >= this.frames;
        }

        public void Finish()
        {
            this.frame = this.frames;
        }

        public bool IsPlaying()
        {
            return this.playing;
        }

        public void NextFrame()
        {
            if (IsPlaying())
            {
                this.frame += 1;
                if (IsFinished())
                {
                    Stop();
                    return;
                }
                Handle(this.frame);
            }
        }

        public string GetName()
        {
            return this.name;
        }

        public int GetCurrentFrame()
        {
            return this.frame;
        }

        public LTexture GetCurrentFrameImage()
        {
            return GetMovie().GetFrameImage();
        }

        public int CountFrames()
        {
            return this.frames;
        }

        public Movie GetMovie()
        {
            return this.movie;
        }

        public void SetMovie(Movie m)
        {
            this.movie = m;
        }

        public bool InFrameRange(int start, int end)
        {
            return (this.frame >= start) && (this.frame <= end);
        }

        public void Forward(int frames)
        {
            this.frame += frames;
        }

        public abstract void Dispose();

    }

}
